package org.jeecg.modules.lab.chartAnaly.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.lab.chartAnaly.service.ChartAnalyService;
import org.jeecg.modules.lab.chartAnaly.vo.SbDeviceVo;
import org.jeecg.modules.lab.entrust.entity.LabEntrustSample;
import org.jeecg.modules.lab.entrust.entity.LabWentrust;
import org.jeecg.modules.lab.entrust.vo.LabEntrustPage;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 图形报表统计
 * @Author: jeecg-boot
 * @Date: 2025-02-24
 * @Version: V1.0
 */
@Api(tags = "图形报表统计")
@RestController
@RequestMapping("/labchart/chartAnaly")
@Slf4j
public class ChartAnalyController extends JeecgController<SbDeviceVo, ChartAnalyService> {

    @Autowired
    private ChartAnalyService chartAnalyService;

    /**
     * 设备使用率统计
     * @param year
     * @return
     */
    @ApiOperation(value = "图形报表统计-设备使用率统计", notes = "图形报表统计-设备使用率统计")
    @GetMapping("/getDevAnalyData")
    public Result getDevAnalyData(@RequestParam(name = "year") String year, @RequestParam(name = "devNumber", defaultValue = "") String devNumber, @RequestParam(name = "devName", defaultValue = "") String devName, @RequestParam(name = "status", defaultValue = "") String status) throws ParseException {
        List<SbDeviceVo> deviceVoList = chartAnalyService.getDevUseAnalyList(year, devNumber, devName, status);
        return Result.ok(deviceVoList);
    }

    /**
     * 导出
     * @param year
     * @param devNumber
     * @param devName
     * @param status
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(@RequestParam(name = "year") String year, @RequestParam(name = "devNumber", defaultValue = "") String devNumber, @RequestParam(name = "devName", defaultValue = "") String devName, @RequestParam(name = "status", defaultValue = "") String status) throws ParseException {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Date day = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //Step.1 获取导出数据
        List<SbDeviceVo> deviceVoList = chartAnalyService.getDevUseAnalyList(year, devNumber, devName, status);
        // Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "设备使用率");
        mv.addObject(NormalExcelConstants.CLASS, SbDeviceVo.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("设备使用率数据", "导出人:" + sysUser.getRealname() + "         导出时间:" + sdf.format(day), "设备使用率"));
        mv.addObject(NormalExcelConstants.DATA_LIST, deviceVoList);
        return mv;
    }
}
